﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace DataStudio
{
    public partial class Index : Form
    {
        public Index()
        {
            InitializeComponent();
        }

        private void Index_Load(object sender, EventArgs e)
        {
            this.Loaded();
        }


        public void Loaded()
        {
            this.LoadMenu();
        }

        private void LoadMenu()
        {
            this.tree.Nodes.Clear();
            TreeNode root = new TreeNode("医院管理系统", 0, 1);
            this.InitlizeTreeMenu(root, null);
            this.tree.Nodes.Add(root);
            root.Expand();
        }

        private void InitlizeTreeMenu(TreeNode node, Group Group)
        {
            IList<Group> GroupList = (new Group()).GetAllGroup();
            foreach (Group var in GroupList)
            {
                TreeNode subNode = new TreeNode(var.Name, 0, 1);
                this.IntalizeTree(var, subNode);
                subNode.Tag = var;
                node.Nodes.Add(subNode);
            }
        }

        protected void IntalizeTree(Group group,TreeNode root)
        {
            IList<GroupModule> moudel = (new GroupModule()).GetListByGroupID(group.ID);

            foreach (GroupModule m in moudel)
            {
                TreeNode node = new TreeNode(m.Name, 2, 3);
                node.Tag = m;
                root.Nodes.Add(node);
            }
        }

        private void tree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                if (e.Node.Tag is GroupModule)
                {
                    GroupModule module = e.Node.Tag as GroupModule;

                    if (Modules.hadModule(module.Type))
                    {

                    }
                    else
                    {
                        try
                        {
                            LoadMod(module);
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
        }

        public void LoadMod(GroupModule module)
        {
            try
            {
                string path = Environment.CurrentDirectory + "\\" + module.Assembly + ".dll";
                if (System.IO.File.Exists(path))
                {
                    Loader f = new Loader(module.Assembly, module.Type);
                    f.MdiParent = this.ParentForm;
                    f.Dock = DockStyle.Fill;
                    f.Text = module.Name;
                    f.Show();
                    string type = module.Type;
                    Modules.opens.Add(type);
                }
                else
                {
                    MessageBox.Show(module.Assembly + "程序集不存在");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private object LoadModule(string name, string assemmby, string type)
        {
            object obj = GetInstanceByFile(assemmby, type);
            if (obj == null)
            {
                throw new Exception("无法加载“" + name + "”，请通知您的系统管理人员。");
            }
            return obj;
        }


        private object GetInstanceByFile(string assemmby, string typename)
        {
            object obj;

            try
            {
                obj = Assembly.Load(assemmby).CreateInstance(typename);
            }

            catch (Exception e)
            {
                throw new Exception("动态创建实例失败 " + assemmby, e);
            }
            return obj;
        }

        private object GetInstanceByName(string className)
        {
            object obj;

            try
            {
                obj = Activator.CreateInstance(Type.GetType(className));
            }

            catch (Exception e)
            {
                throw new Exception("动态创建实例失败 " + className, e);
            }
            return obj;
        }

    }
}
